package com.app.piemoment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.piemoment.api.ApiResult;
import com.app.piemoment.api.ApiReturnResultListener;
import com.app.piemoment.api.UserAPI;
import com.app.piemoment.entity.City;
import com.app.piemoment.entity.CityListener;
import com.app.piemoment.entity.CitySelectDialog;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.entity.PieUser;
import com.app.piemoment.view.PmDialog;

public class ComplateUserInfo4Activity extends Activity {
	public int sex = 1;
	EditText height_min, height_ideal, height_max, age_diff_min,
			age_diff_ideal, age_diff_max;
	Button ok_Button, degree_min, degree_ideal, dgree_max, location_ideal,
			location_distance;
	SharedPreferences userInfo;
	public String[] cityArea = new String[] { "同城市", "同省", "同国家", "同星球" };

	private void selectEdu(final TextView button, final String[] temp) {
		PmDialog.createListView(ComplateUserInfo4Activity.this, temp, 0,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						button.setText(temp[which]);
					}
				});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regedit_info_step4);
		userInfo = getSharedPreferences(BaseActivity.PREF_NAME,
				MODE_WORLD_READABLE);
		height_min = (EditText) findViewById(R.id.height_min);
		height_ideal = (EditText) findViewById(R.id.stature_best);
		height_max = (EditText) findViewById(R.id.stature_high);
		age_diff_min = (EditText) findViewById(R.id.age_low);
		age_diff_ideal = (EditText) findViewById(R.id.age_best);
		age_diff_max = (EditText) findViewById(R.id.age_high);

		degree_min = (Button) findViewById(R.id.education_low);
		degree_min.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				selectEdu(degree_min, ComplateUserInfo2Activity.edu);
			}
		});
		degree_ideal = (Button) findViewById(R.id.education_best);

		degree_ideal.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				selectEdu(degree_ideal, ComplateUserInfo2Activity.edu);
			}
		});

		dgree_max = (Button) findViewById(R.id.education_high);
		dgree_max.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				selectEdu(dgree_max, ComplateUserInfo2Activity.edu);
			}
		});

		location_ideal = (Button) findViewById(R.id.location_ideal);
		location_ideal.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				City startCity = new City();
				CitySelectDialog cityDialog = new CitySelectDialog(
						ComplateUserInfo4Activity.this, 0, startCity,
						new CityListener() {

							@Override
							public void onCityResult(City city, int whichOne) {
								location_ideal.setText(city.getLastSpace());
							}
						});

			}
		});

		location_distance = (Button) findViewById(R.id.location_distance);
		location_distance.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PmDialog.createListView(ComplateUserInfo4Activity.this,
						cityArea, 0, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								location_distance.setText(cityArea[which]);
							}
						});

			}
		});

		ok_Button = (Button) findViewById(R.id.ok_button);
		ok_Button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				userInfo.edit().putString(PieUser.height_min_s,
						height_min.getText().toString()).commit();
				PieUser.getMyStaticInof().setHeight_min(
						height_min.getText().toString());

				userInfo.edit().putString(PieUser.height_ideal_s,
						height_ideal.getText().toString()).commit();

				PieUser.getMyStaticInof().setHeight_ideal(
						height_ideal.getText().toString());

				userInfo.edit().putString(PieUser.height_max_s,
						height_max.getText().toString()).commit();

				PieUser.getMyStaticInof().setHeight_max(
						height_max.getText().toString());

				userInfo.edit().putString(PieUser.age_diff_min_s,
						age_diff_min.getText().toString()).commit();

				PieUser.getMyStaticInof().setAge_diff_min(
						age_diff_min.getText().toString());

				userInfo.edit().putString(PieUser.age_diff_ideal_s,
						age_diff_ideal.getText().toString()).commit();

				PieUser.getMyStaticInof().setAge_diff_ideal(
						age_diff_ideal.getText().toString());

				userInfo.edit().putString(PieUser.age_diff_max_s,
						age_diff_max.getText().toString()).commit();

				PieUser.getMyStaticInof().setAge_diff_max(
						age_diff_max.getText().toString());

				userInfo.edit().putString(PieUser.degree_min_s,
						degree_min.getText().toString()).commit();

				PieUser.getMyStaticInof().setDegree_min(
						degree_min.getText().toString());

				userInfo.edit().putString(PieUser.degree_ideal_s,
						degree_ideal.getText().toString()).commit();

				PieUser.getMyStaticInof().setDegree_ideal(
						degree_ideal.getText().toString());

				userInfo.edit().putString(PieUser.dgree_max_s,
						dgree_max.getText().toString()).commit();

				PieUser.getMyStaticInof().setDgree_max(
						dgree_max.getText().toString());

				userInfo.edit().putString(PieUser.location_ideal_s,
						location_ideal.getText().toString()).commit();

				PieUser.getMyStaticInof().setLocation_ideal(
						location_ideal.getText().toString());

				userInfo.edit().putString(PieUser.location_distance_s,
						location_distance.getText().toString()).commit();

				PieUser.getMyStaticInof().setLocation_distance(
						location_distance.getText().toString());

				setExpectations(PieUser.getMyStaticInof());

			}
		});

	}

	protected static final int LOAD_PROGRESS = 1111;
	JSONObject jsonEntity;

	private void setExpectations(PieUser pieuser) {
		UserAPI api = new UserAPI(this);

		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(LOAD_PROGRESS);
				if (ApiResult.RESULT_OK == apiResult.getResultCode()) {
					jsonEntity = apiResult.getJsonEntity();
					try {
						if (jsonEntity != null) {
							if (jsonEntity.has("status")) {
								String result = jsonEntity.getString("status");
								if (ApiResult.Success.equals(result)) {
									if (jsonEntity.has("data")) {
										JSONArray array = jsonEntity
												.getJSONArray("data");

									}

								}

							}

						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				Intent intent = new Intent();
				intent.setClass(ComplateUserInfo4Activity.this,
						ComplateUserInfo5Activity.class);
				startActivity(intent);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(LOAD_PROGRESS);

			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {

				dismissDialog(LOAD_PROGRESS);

				try {
					Toast.makeText(
							ComplateUserInfo4Activity.this,
							apiResult.getJsonEntity().getJSONArray("message")
									.getJSONObject(0).getString("message"),
							Toast.LENGTH_LONG).show();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		api.setExpectationsInfo(1, ComplateUserInfo4Activity.this, pieuser,
				LoginInfo.getInstance().getPuid() + "");
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOAD_PROGRESS:
			PmDialog dialog = new PmDialog(this);
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}
}
